#include<bits/stdc++.h>
#define int long long
#define IOS ios::sync_with_stdio(false), cin.tie(0), cout.tie(0)
using namespace std;

map<string, set<string>>mp;

string zh(string s) {
	string ret = "";
	for(int i = 0; i < s.size(); ++ i ) {
		if(i == 0) {
			ret += s[i];
		} else if(s[i - 1] == ' ') {
			if(s[i] != ' ')
			ret += s[i];
		}
	}
	return ret;
}

signed main() {
	int n;
	cin >> n;
	string p;
	getline(cin, p);
	
	for(int i = 1; i <= n; ++ i ) {
		string s;
		getline(cin, s);
		mp[zh(s)].insert(s);
	}
	int m;
	cin >> m;
	getline(cin, p);
	while(m -- ) {
		string s;
		getline(cin, s);
		set<string>t = mp[zh(s)];
		if(t.size() == 0) {
			cout << s << endl;
		} else {
			int idx = 1;
			if(t.size() == 1) {
				for(string tt : t) {
					cout << tt << endl;
				}
			} else {
				for(string tt : t) {
					if(idx == 1) {
						cout << tt;
					} else {
						cout << '|' << tt;
					}
					idx ++ ;
				}
				cout << endl;
		  }
		}
	}
	return 0;
}

